import 'dart:convert';

import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/current_shift_entity.g.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:sp_util/sp_util.dart';

export 'package:flutter_deer/generated/json/current_shift_entity.g.dart';

@JsonSerializable()
class CurrentShiftEntity {
	String archivePlanId = '';
	String createName = '';
	String createTime = '';
	String endTime = '';
	String headUser = '';
	String headUserName = '';
	String id = '';
	int isDeleted = 0;
	bool isErase = false;
	String mchCode = '';
	int mqttSendMsg = 0;
	String startTime = '';
	String updateName = '';
	String updateTime = '';
	String workCycleCode = '';
	int workCycleStatus = 0;

	CurrentShiftEntity();

	factory CurrentShiftEntity.fromJson(Map<String, dynamic> json) => $CurrentShiftEntityFromJson(json);

	Map<String, dynamic> toJson() => $CurrentShiftEntityToJson(this);

	// save
	void save() {
		SpUtil.putObject(Constant.currentShift, toJson());
	}

	// 读取
	static CurrentShiftEntity? get() {
		final String? jsonString = SpUtil.getString(Constant.currentShift);
		if (jsonString?.isNotEmpty!= true) {
			return null;
		}
		try {
			final dynamic data = json.decode(jsonString!);
			if (data is Map<String, dynamic>) {
				return CurrentShiftEntity.fromJson(data);
			} else {
				return null;
			}
		} catch (e) {
			return null;
		}
	}

	@override
	String toString() {
		return jsonEncode(this);
	}
}